package com.education.cms.utils;

import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class DateUtils {
    /**
     * 计算某日期至目前经过的分钟数
     */
    public static Integer getMinuteBetween(Date date){
        DateFormat dateFormat = new SimpleDateFormat("hh-mm-DD");
        String[] time1 = dateFormat.format(date).split("-");
        String[] time2 = dateFormat.format(new Date()).split("-");
        int h1 = Integer.parseInt(time1[0]);
        int h2 = Integer.parseInt(time2[0]);
        int m1 = Integer.parseInt(time1[1]);
        int m2 = Integer.parseInt(time2[1]);
        int d1 = Integer.parseInt(time1[2]);
        int d2 = Integer.parseInt(time2[2]);
        if (d1 != d2){
            return 999;
        } else if (m1 <= m2){
            if (h1 == h2){
                int count = m2 - m1;
                return count;
            }else {
                int count = (h2-h1) * 60 + (m2 - m1);
                return count;
            }
        }else {
            int count = (h2-h1-1) * 60 + (60-m1) + m2;
            return count;
        }
    }
    /**
     * 输入年龄返回出生年份
     */
    public static Integer birthYear(int age) {
        DateFormat format = new SimpleDateFormat("yyyy");
        int birthYear = Integer.parseInt(format.format(new Date())) - age;
        return birthYear;
    }

    /**
     * 获取日期的时间为"00:00:00"
     */
    public static Date dateNoTime(Date date) throws ParseException {
        String formatDate = new SimpleDateFormat("yyyy-MM-DD").format(date);
        DateFormat format =new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
        return format.parse(formatDate+" 00:00:00");
    }
    /**
     * 获取日期的时间为"23:59:59"
     */
    public static Date dateFullTime(Date date) throws ParseException {
        String formatDate = new SimpleDateFormat("yyyy-MM-DD").format(date);
        DateFormat format = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
        return format.parse(formatDate+" 23:59:59");
    }

}
